// pages/indexSec/indexSec.js
let util = require('../../utils/util');
const api = require('../../utils/api');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    proUrls: wx.getStorageSync("furProUrls"),//两张大图
    furItemN: 3,//小图当前数量 
    allDetailUrls: wx.getStorageSync("allDetailUrls"),//所有小图
    proDetailUrls: "",//当前显示的小图
    detailAllN: 1,//小图总数量 
    more: true//判断是否还有更多图
  },
  // 跳转到商品详情页
  toProInfoT(e){
    let id = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: "../productInfo/productInfo?id=" + this.data.proUrls[id].id,
      success(res) {
      }
    })
  },
  toProInfoB(e) {
    let id = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: "../productInfo/productInfo?id=" + this.data.allDetailUrls[id].id,
      success(res) {
      }
    })
  },
  // 查看更多
  seeMore() {
    var that = this;
    if(that.data.furItemN < that.data.detailAllN){
      console.log(that.data.detailAllN);
      that.setData({
        furItemN: that.data.furItemN + 3
      },function(){
        that.setData({
          proDetailUrls: that.data.allDetailUrls.slice(0, that.data.furItemN)
        });
      })
      console.log(that.data.proDetailUrls)
    }else{
      that.setData({
        more: false
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showNavigationBarLoading();
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var that = this;
    // 获取大图片
    if (!that.data.proUrls){
      util.myRequest(api.index, {},
        res => {
          
          that.setData({
            proUrls: res.data.data.newGoodsList.slice(1,3)
          });
          wx.setStorage({
            key: 'proUrls',
            data: res.data.data.newGoodsList.slice(1,3),
          })
        }
      )
    }
    // 获取小图片
    if (!that.data.allDetailUrls){
      util.myRequest(api.goods, {},
        res => {
          wx.hideNavigationBarLoading();
          wx.hideLoading();
          that.setData({
            detailAllN: res.data.data.data.length,
            allDetailUrls: res.data.data.data,
            proDetailUrls: res.data.data.data.slice(0, that.data.furItemN)
          });
          wx.setStorage({
            key: 'allDetailUrls',
            data: res.data.data.data,
          })
        }
      )
    } else {
      wx.hideNavigationBarLoading();
      wx.hideLoading();
      that.setData({
        detailAllN: that.data.allDetailUrls.length,
        proDetailUrls: that.data.allDetailUrls.slice(0, that.data.furItemN)
      });
    }
  },
})